package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.SysKeyTypeInfo;
import com.emep.changzhi.analyse.repository.basics.SysKeyTypeRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import com.emep.changzhi.analyse.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xy
 * @Description: 重点类型业务层
 * @date 2018/2/26 10:56
 * @version V1.0
 */
@Service
public class SysKeyTypeService {
    @Resource
    private SysKeyTypeRepository keyTypeRepository;
    private static final Logger logger = LoggerFactory.getLogger(SysAreaInfoService.class);


    /**
     *
     * 新增重点类型
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysKeyTypeInfo save(Map<String, Object> payload) {
        SysKeyTypeInfo keyTypeInfo = null;
        try {
            SysKeyTypeInfo keyTypeInfo1 = (SysKeyTypeInfo) MapUtils.convertMap(SysKeyTypeInfo.class,payload);
            keyTypeInfo=keyTypeRepository.save(keyTypeInfo1);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return keyTypeInfo;
    }

    /**
     *
     * 根据id 删除重点类型
     *
     * @param id
     */
    public boolean deleteKeyType(String id) {
        boolean result = false;
        try {
            Integer result1 = keyTypeRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有重点类型
     *
     * @return
     */
    public List<SysKeyTypeInfo> findAllKeyTypes() {
        List<SysKeyTypeInfo> keyTypeInfo = null;
        try {
            keyTypeInfo =  keyTypeRepository.findAll();
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return keyTypeInfo;
    }

    /**
     *
     * 根据id查询重点类型
     *
     * @param id
     * @return
     */
    public SysKeyTypeInfo findKeyTypeById(String id){
        SysKeyTypeInfo keyTypeInfo = null;
        try {
            keyTypeInfo = keyTypeRepository.findKeyTypeById(id);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return keyTypeInfo;
    }

    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysKeyTypeInfo> findAll(Integer pageNo, Integer pageSize){
        try {
            Pageable pageable = new PageRequest(pageNo-1, pageSize);
            return keyTypeRepository.findAll(pageable);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        }
    }
    /**
     *
     * @param page 页数
     * @param size 数量
     * @param keyTypeInfo 参数
     * @return
     */
    public Page<SysKeyTypeInfo> findBookCriteria(Integer page, Integer size, final SysKeyTypeInfo keyTypeInfo) {
        //规格定义
        Specification<SysKeyTypeInfo> specification = new Specification<SysKeyTypeInfo>() {
            /**
             * 构造断言
             * @param root 实体对象引用
             * @param query 规则查询对象
             * @param cb 规则构建对象
             * @return 断言
             */
            @Override
            public Predicate toPredicate(Root<SysKeyTypeInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>(); //所有的断言
                if(!StringUtils.isEmpty(keyTypeInfo.getKeyTypeName())){
                    Predicate likeNickName = cb.equal(root.get("keyTypeName").as(String.class),keyTypeInfo.getKeyTypeName());
                    predicates.add(likeNickName);
                }

                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
        //分页信息
        Pageable pageable = new PageRequest(page-1,size); //页码：前端从1开始，jpa从0开始，做个转换
        //查询
        return keyTypeRepository.findAll(specification,pageable);
    }
}
